<?PHP
error_reporting(0);

function eveTime() {
	return time() + 18000;
}

function renderName($charID) {
	$query = "SELECT * FROM characters WHERE charID = '".$charID."'";
	$results = mysql_query($query);
	
	if ($char = mysql_fetch_array($results)) {
		$nameSlash = $char['name'];
		unset($flags);
		
		/*$altQuery = "SELECT characters.charID, characters.name, characters.lastSeen
			FROM characters, alts
			WHERE alts.charID = characters.charID
			AND alts.altOf = '{$charID}'
			AND characters.inCorp = 1
			ORDER BY characters.name";*/
		$altQuery = "SELECT distinct characters.charID, characters.name, characters.lastSeen
			FROM characters JOIN alts
			ON alts.charID = characters.charID
			WHERE alts.altOf = '{$charID}'
			AND characters.inCorp = 1
			ORDER BY characters.name";

		//this one actually works
		/*$altQuery = "SELECT characters.name, characters.charID, characters.lastSeen
		                FROM characters WHERE charID IN(SELECT charID
				FROM alts WHERE altOf = '".$charID."')
				AND characters.inCorp = 1
				ORDER BY characters.name";*/
		$altResults = mysql_query($altQuery);
		$numAlts = mysql_num_rows($altResults);
		
		/*** RENDER NAME ***/
		echo "<a href=\"characters.php?id={$char['charID']}\">{$char['name']}</a> ";
		if ($numAlts == 1) echo "- <a href=\"javascript:toggleDiv('altWin{$char['charID']}')\">1 alt</a> ";
		elseif ($numAlts != 0) echo "- <a href=\"javascript:toggleDiv('altWin{$char['charID']}')\">$numAlts alts</a> ";
		
		/***RENDER FLAGS ***/
		if ($char['afk'] == 1) {
			echo "<img src=\"images/isafk.gif\" alt=\"{$char['afkText']}\"> ";
			$isFlags = 1;
		}
		if ($char['notes'] != NULL) {
			echo '<img src="images/iscustom.gif"> ';
			$isFlags = 1;
		}
		if ($char['investigate'] == 1) {
			echo '<img src="images/iswarning.gif"> ';
			$isFlags = 1;
		}
		if ($char['inactive'] == 1) {
			echo '<img src="images/ismia.gif"> ';
			$isFlags = 1;
		}
		
		if ($isFlags == 1) echo "| ";
		
		/*** RENDER ACTIONS ***/
		if ($char['afk'] == 1) {
			$flags['afk'] = 'CHECKED';
			$flags['afkText'] = addslashes($char['afkText']);
		}
		$result = mysql_query("SELECT * FROM Jobs WHERE charID = '".$charID."'");
		$row = mysql_fetch_array($result);
		if ($char['investigate'] == 1) $flags['investigate'] = 'CHECKED';
		if ($char['posgunner'] == 1) $flags['posgunner'] = 'CHECKED';
		if ($row['pos'] == 1) $flags['posd'] = 'CHECKED';
		if ($row['exempt'] == 1) $flags['exempt'] = 'CHECKED';
		if ($row['legacy'] == 1) $flags['legacy'] = 'CHECKED';
		if ($row['probation'] == 1) $flags['probation'] = 'CHECKED';
		$flags['notes'] = str_replace('<br />', '\n', stripslashes($char['notes']));
		//allowing both kinds of quotes to be used in the notes section
		$flags['notes'] = str_replace('\'','c39c',$flags['notes']);
		$flags['notes'] = str_replace('"','c34c',$flags['notes']);
		$nameSlash = str_replace('\'','c39c',$nameSlash);
		$nameSlash = str_replace('"','c34c',$nameSlash);
		
		if ($numAlts == 0) echo "<a href=\"javascript:showIsAltWin('{$nameSlash}', {$char['charID']})\"><img src=\"images/isalt.gif\"></a> ";
		
		$result2 = mysql_query("SELECT altOf FROM alts WHERE charID = '".$char['charID']."'");
		$row2 = mysql_fetch_array($result2);
		$main = $row2[0];
		if(empty($main)) $main = $char['charID'];
		echo "<a href=\"javascript:showFlagWin('{$nameSlash}', '".$main."', {$char['charID']},
			'{$flags['afk']}', '{$flags['afkText']}', '{$char['tz']}', '{$char['carrier']}',
			'{$char['dread']}', '{$flags['investigate']}', '{$flags['posgunner']}',
			'{$flags['notes']}', '{$_SERVER['PHP_SELF']}', '".$flags['posd']."', '".$flags['exempt']."', '".$flags['legacy']."', '".$flags['probation']."')\"><img src=\"images/plusflag.gif\"></a> ";
		
		/*** RENDER POP UPS ***/
		if ($numAlts != 0) {
			echo "<div class=\"altWin\" id=\"altWin{$char['charID']}\" style=\"display: none\">";
			while ($alts = mysql_fetch_array($altResults)) {
				echo "<li>{$alts['name']} <a href=\"isAlt.php?charID={$alts['charID']}
					&altOf={$char['charID']}&del=1\"><img src=\"images/notalt.gif\"></a></li>";
			}
			echo "</div>";
		}
	}
}
?>
